import 'package:bt_dsp_88/model/model_send.dart';
import 'package:flutter/material.dart';
import 'package:flutter_kit_plus/flutter_kit.dart';

import '../../bluetooth/send_data.dart';
import '../../class/sum/input.dart';
import '../../commont/base_controller.dart';
import '../../model/model.dart';
import '../../model/model_default.dart';
import '../../notify/globalState.dart';
import '../dialog/change_ch_name.dart';
import '../dialog/select_copy.dart';
import 'delay.dart';
import 'output.dart';

class SumController extends BaseController {
  SumController({super.key});

  @override
  State<SumController> createState() => kstate = _SumControllerState();
}

class _SumControllerState extends BaseControllerState<SumController> {
  late final List<ChannelModel> _output_list;
  late final List<ChannelModel> _input1_list;
  late final List<ChannelModel> _input2_list;

  @override
  void initState() {
    super.initState();
    select_list = globalModel.ch_list.map(($0) => false).toList();

    _output_list = List.generate(8, ($0) => initCH($0));
    _input1_list = List.generate(8, ($0) => initCH($0));
    _input2_list = List.generate(8, ($0) => initCH($0));
  }

  ///改变delay的方法
  void _change_Delay(ChannelModel ch) {
    setState(() {
      unitChannel(ch);
      select_list = globalModel.ch_list.map(($0) => $0.select).toList();
    });
  }

  ///改变Assign | Custom | Copy | Erase
  void _changeType(String type) {
    if (globalModel.input_type == type || globalModel.copy_stype == type) {
      return;
    }
    switch (type) {
      case 'Assign':
        globalModel.input_type = type;
        for (var ch in _output_list) {
          ch.select = true;
          ch.input_list.clear();
          ch.input_list.addAll(ch.input_assign_list);
        }
        for (var ch in globalModel.ch_list) {
          ch.input_list.clear();
          ch.input_list.addAll(ch.input_assign_list);
          _send_data('SUM', ch);
        }
        for (var ch in _input1_list) {
          ch.enable = false;
        }

      case 'Custom':
        globalModel.input_type = type;
        for (var ch in _output_list) {
          ch.select = false;
          ch.input_custom_list.clear();
          ch.input_custom_list.addAll(List.generate(8, ($0) => 0).toList());
          ch.input_list.clear();
          ch.input_list.addAll(ch.input_custom_list);
        }
        for (var ch in globalModel.ch_list) {
          ch.input_custom_list.clear();
          ch.input_custom_list.addAll(List.generate(8, ($0) => 0).toList());
          ch.input_list.clear();
          ch.input_list.addAll(ch.input_custom_list);
          _send_data('SUM', ch);
        }
        for (var ch in _input1_list) {
          ch.enable = false;
        }
      case 'Copy':
        globalModel.copy_stype = type;
        for (var $0 in _input2_list) {
          $0.enable = true;
          $0.select = false;
        }
      case 'Erase':
        globalModel.copy_stype = type;
        for (var $0 in _input2_list) {
          $0.enable = false;
          $0.select = false;
        }
      default:
    }
    setState(() {});
  }

  ///修改通道名字
  void _changeName(ChannelModel ch, bool isOutput) {
    ChangeNameView changename = ChangeNameView();
    changename.show(context);
    changename.name = ch.name;
    changename.onChange = (text) {
      ch.name = text;
      if (isOutput) {
        for (var $0 in _input2_list) {
          $0.name = $0.num == ch.num ? text : $0.name;
        }
        for (var $0 in globalModel.ch_list) {
          $0.name = $0.num == ch.num ? text : $0.name;
        }
        globalState.state = NotifyData(key: '全部刷新');
      } else {
        setState(() {});
      }
    };
  }

  @override
  void realod() {
    for (var ch in _output_list) {
      ch.enable = globalModel.enable;
      ch.select = false;
      ch.input_list.clear();

      ch.input_list.addAll(globalModel.ch_list[ch.num - 1].input_list);
      ch.input_custom_list.clear();
      ch.input_custom_list
          .addAll(globalModel.ch_list[ch.num - 1].input_custom_list);
    }
    for (var ch in _input1_list) {
      ch.enable = globalModel.enable;
      ch.select = false;
    }
    for (var ch in _input2_list) {
      ch.enable = globalModel.copy_stype == 'Copy' && globalModel.enable;
    }
    super.realod();
  }

  @override
  Widget buildContent(BuildContext context) {
    return Column(children: [
      OutputView(
          title: 'Output Select',
          ch_list: _output_list,
          onTap: (p0) {
            for (var $0 in _output_list) {
              $0.select = $0.name == p0.name;
            }
            for (var $0 in _input1_list) {
              $0.enable = true;
              $0.select = p0.select && p0.input_custom_list.contains($0.num);
            }
            setState(() {});
          },
          onLongPress: (p0) => _changeName(p0, true)),
      InputView(
          title: 'Input Select',
          lefttitle: 'Assign',
          righttitle: 'Custom',
          ch_list: _input1_list,
          onClick: _changeType,
          onTap: (p0) {
            p0.select = !p0.select;

            for (var $0 in _output_list) {
              if ($0.select) {
                $0.input_custom_list[p0.num - 1] = p0.select ? p0.num : 0;
                $0.input_list.clear();
                $0.input_list.addAll($0.input_custom_list);

                ///更新全局通道
                ChannelModel ch = globalModel.ch_list[$0.num - 1];
                ch.input_custom_list[p0.num - 1] = p0.select ? p0.num : 0;
                ch.input_list.clear();
                ch.input_list.addAll(ch.input_custom_list);
                _send_data('SUM', ch);
              }
            }

            setState(() {});
          },
          onLongPress: (p0) => _changeName(p0, false)),
      InputView(
        title: '',
        lefttitle: 'Copy',
        righttitle: 'Erase',
        ch_list: _input2_list,
        onClick: _changeType,
        onTap: (p0) {
          /*
          if (globalModel.input_type != 'Custom') {
            EasyLoading.showInfo('Please set the input be custom');
            return;
          }
          */
          for (var $0 in _input2_list) {
            $0.select = p0.name == $0.name;
          }
          setState(() {});

          for (var from in _output_list) {
            if (from.num == p0.num) {
              SelectCopyView copy_view =
                  SelectCopyView(ch: from, list: _output_list);
              copy_view.show(context);
              copy_view.onChange = (name) {
                for (var to in _output_list) {
                  if (to.name == name) {
                    ChannelModel ch_to = globalModel.ch_list[to.num - 1];
                    ChannelModel ch_from = globalModel.ch_list[from.num - 1];

                    ///copy全通道，除了合频的其他数据
                    String hex = ch_from.data;
                    List<String> str_list = hex.split('|');
                    str_list.removeWhere(
                        ($0) => $0.contains('AA55${ble_sum.bytes.toHexstr()}'));
                    for (var byte in str_list) {
                      ch_to.dataToModel(byte.bytes);
                    }

                    ///转换完成后，要更新设备
                    ch_to.sendToDevice(containSum: false);

/* copy,不copy合频，
                    ///组件上显示的通道
                    to.input_custom_list.clear();
                    to.input_custom_list.addAll(from.input_custom_list);
                    to.input_list.clear();
                    to.input_list.addAll(from.input_custom_list);

                    ///更新全局真实的通道
                    ///1，copy 合频混音
                    ch_to.input_custom_list.clear();
                    ch_to.input_custom_list.addAll(from.input_custom_list);
                    ch_to.input_list.clear();
                    ch_to.input_list.addAll(from.input_custom_list);

                    ///2，copy其他的channel的正常属性
                    ch_to.mute = ch_from.mute;
                    ch_to.phase = ch_from.phase;
                    ch_to.db = ch_from.db;
                    ch_to.delay = ch_from.delay;
                    ch_to.delay_type = ch_from.delay_type;
                    ch_to.high_pass = ch_from.high_pass!.copy;
                    ch_to.low_pass = ch_from.low_pass!.copy;
                    List<SignModel> sign_list =
                        ch_from.sign_list.map(($0) => $0.copy).toList();
                    ch_to.sign_list.clear();
                    ch_to.sign_list.addAll(sign_list);
                    */
                  }
                }
                setState(() {});
              };
            }
          }
        },
      ),
      DelayView(
        ch_select: current_ch,
        ch_list: globalModel.ch_list.toList(),
        title: 'Time Delay',
        onClick: _change_Delay,
        onCompletion: (ch) {
          _send_data('DELAY', ch);
        },
      ),
    ]);
  }

  @override
  void copy(ChannelModel model, ChannelModel from) {}
}

///发送数据
extension SumControllerExtension on _SumControllerState {
  void _send_data(String tag, ChannelModel ch) {
    if ((tag != 'SUM' && tag != 'DELAY') || ch.name == '') return;
    globalState.state = NotifyData(key: tag, body: ch);
  }
}
